/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 11, 2012  10:25:04 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.commons.vendor
 * File Name : VendormasterProcessing.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;
import jpa.entitys.synchro.staging.MmVendorMasInb;
import jpa.service.synchro.staging.MmVendorMasInbServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import apps.sessionFactory;
import apps.promis.in.common.SupplierMasterAction;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:38:19 PM 
 * CreateBy=Chatree Siangklom
 * 
 * VendorMasterProcessingThread
 * TODO
 */
public class VendorMasterProcessingThread extends Thread {
	
	private static final Logger log = Logger.getLogger(VendorMasterProcessingThread.class);
	private MmVendorMasInb mmVendorMasInb = null;
	public VendorMasterProcessingThread(MmVendorMasInb mmVendorMasInb) {
		this.mmVendorMasInb = mmVendorMasInb;
	}
	
	public VendorMasterProcessingThread(){}
	
	/**
	 * (Javadoc)
	 * @see java.lang.Thread#run()
	 * Date =Nov 13, 2013 : 2:38:29 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public void run()
    {
        try {
        	
        	MmVendorMasInbServices mmVendorMasInbServ = new MmVendorMasInbServices(sessionFactory.CreateEmSynchro());
        	
        	try{
        		
				new SupplierMasterAction().InsertOrUpdated(mmVendorMasInb);
								
				mmVendorMasInb.setIssync("Y");
				mmVendorMasInb.setMsgCode("S");
				mmVendorMasInb.setMsgDesc("Success");
				mmVendorMasInb.setMsgActResult(null);
				mmVendorMasInbServ.Updated(mmVendorMasInb);
				
			}catch(Exception ex)
			{
				mmVendorMasInb.setIssync("N");
				mmVendorMasInb.setMsgCode("E");
				mmVendorMasInb.setMsgDesc("Error");
				mmVendorMasInb.setMsgActResult(ex.getMessage());
				mmVendorMasInbServ.Updated(mmVendorMasInb);
			}		
        	
			sleep(500);
			
		} catch (Exception e) {
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
    }
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:38:34 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	public void proc() throws Exception {
		MmVendorMasInbServices mmVendorMasInbServ = new MmVendorMasInbServices(sessionFactory.CreateEmSynchro());
		List<MmVendorMasInb> lst = null;
		
		String issync = null;
		if(!CommonService.IsNull(issync))
			lst = mmVendorMasInbServ.findAllIsSync("N");
		else
			lst = mmVendorMasInbServ.findByAll();
		
		if(!CommonService.IsNull(lst)){
			int i = 0;
			int count = 0;
			for(MmVendorMasInb mmVendorMasInb : lst)
			{
				i++;
				count++;
				log.info(" Row Count = " + i);
				new VendorMasterProcessingThread(mmVendorMasInb).start();
				
				if(count == 40){
					count = 0;
					sleep(3000);
				}
			}
		}
	}
}
